<!--安全标准化建设情况统计表 standardSafeRpt-->
<template>
    <Layout>

        <Form ref="form1" :model="dataToQuery" :label-width="120">
            <Row v-show="!switchFlag" class="query-form" >
                <Col span="8">
                    <FormItem label="企业名称：" prop="order">
                        <cascader-dom class="query-form-el" ref='queryOrgCascader' @setOrgInfo='setQueryCompanyName' :orgTree="true" :setCurrentName="{label:dataToQuery.companyName,value:dataToQuery.companyId}"></cascader-dom>
                    </FormItem>
                </Col>
                <Col span="8">
                    <FormItem label="填表时间：" prop="content" >
                        <DatePicker class="query-form-el" v-model="dataToQuery.dateRange" format="yyyy-MM-dd" type="daterange" placement="bottom-end" placeholder="请选择查询时间段"
                                    @on-change="onSelectQueryDateRange"></DatePicker>
                    </FormItem>
                </Col>
                <Col span="8">
                    <Button @click="switchQuery(true)" class="ivu-btn-switch" type="text">展开∨</Button>
                    <Button @click="reset" class="ivu-btn-reset"  >重置</Button>
                    <Button type="primary" class="ivu-btn-query"   icon="md-search"  @click="query">查询</Button>
                </Col>
            </Row>
            <Row v-show="switchFlag" class="query-form">
                <Col span="8">
                    <FormItem label="企业名称：" prop="order">
                        <cascader-dom class="query-form-el" ref='queryOrgCascader1' @setOrgInfo='setQueryCompanyName' :orgTree="true" :setCurrentName="{label:dataToQuery.companyName,value:dataToQuery.companyId}"></cascader-dom>
                    </FormItem>
                </Col>
                <Col span="8">
                    <FormItem label="填表时间：" prop="content" >
                        <DatePicker class="query-form-el" v-model="dataToQuery.dateRange" format="yyyy-MM-dd" type="daterange" placement="bottom-end" placeholder="请选择查询时间段"
                                    @on-change="onSelectQueryDateRange"></DatePicker>
                    </FormItem>
                </Col>
                <Col span="8">
                    <FormItem label="建设达标等级：" prop="standardRand">
                        <Input v-model='dataToQuery.standardRand' class="query-form-el"></Input>
                    </FormItem>
                </Col>
            </Row>
            <Row v-show="switchFlag" class="query-form">
                <Col span="8">
                    <FormItem label="组织评审单位：" prop="assessOrganizer">
                        <Input v-model='dataToQuery.assessOrganizer' class="query-form-el"></Input>
                    </FormItem>
                </Col>
                <Col span="8"></Col>
                <Col span="8" offset="8">
                    <Button @click="switchQuery(false)" class="ivu-btn-switch" type="text">收起∧</Button>
                    <Button @click="reset" class="ivu-btn-reset">重置</Button>
                    <Button class="ivu-btn-query" icon="md-search" @click="query">查询</Button>
                </Col>
            </Row>
            <Row class="query-form">
                <Col span="24">
                    <Button size="large"  class="ivu-btn-add" icon="md-add"  @click="addInfo">新增</Button>
                    <Button type="primary" class="ivu-btn-add"   icon="md-cloud-download"  @click="exportFormSubmit">导出</Button>
                </Col>
            </Row>
        </Form>
        <Table ref='table' :height="tableHeight"  no-data-text="没有查询到相关数据!" @on-selection-change = 'getSelectRows' :loading='pageStatus.loading'
               border :columns="columns1"  :data="dataTable" >
        </Table>
        <Row style="margin-right:10px;">
            <Col span="24" >
                <Page style="float: right" :total="pagedata.total" :page-size="pagedata.pageSize" @on-change="pageChange" @on-page-size-change="pageSizeChange" :page-size-opts="pageSizeArray" show-total show-sizer show-elevator />
            </Col>
        </Row>
        <Modal  title="安全标准化建设情况统计表" :transfer=false :mask-closable="false" v-model="dialogShow" v-if="dataToView && dialogShow"  :width=800 :styles="{height:'620px'}"  class-name='limit-height-dialog'>
            <Row>
                <Col span="24">
                    <Form ref="dialogForm" :class="mode === 'view'?'modal-view-detail':''" :show-message="false" :model="dataToView" :rules="mode === 'view'?{}:ruleCustom" :label-width="mode === 'view'?160:null" :label-position="mode === 'view'?'right':'top'">
                      <Row type="flex" justify="space-around" class="code-row-bg">
                        <Col span="23" >
                          <FormItem label='企业名称：' prop='companyId' span="12">
                            <div v-if="mode === 'view'">{{dataToView.companyName}}</div>
                            <cascader-dom v-else ref='orgCascader' @setOrgInfo='setCompanyId' @setOrgname="setCompanyName" :orgTree="true" :currentOrgId = "dataToView.companyId" :setCurrentName="{label:dataToView.companyName,value:dataToView.companyId}" ></cascader-dom>
                          </FormItem>
                        </Col>
                      </Row>
                      <Row type="flex" justify="space-around" class="code-row-bg">
                        <Col span="11" >
                          <FormItem label='所在地：' prop='companyAddress'>
                            <div v-if="mode === 'view'">{{areaCodeToAreaName(dataToView.companyAddress)}}</div>
                            <Cascader v-else :data="tempAreaTree" v-model="dataToView.companyAddressArr" change-on-select :transfer="true"
                                      :load-data="areaTreeLoadData" :render-format="labels=>labels.join('·')"  :clearable='false'
                                      @on-change="(value,selectedData)=>{$set(dataToView,'companyAddress',value.join(','))}"></Cascader>
                          </FormItem>
                        </Col>
                          <Col span="11" >
                              <FormItem label='已通过安全标准化建设达标等级：' prop='standardRand'>
                                  <div v-if="mode === 'view'">{{dataToView.standardRand}}</div>
                                  <Input v-else  type='text' v-model='dataToView.standardRand'></Input>
                              </FormItem>
                          </Col>
                      </Row>
                      <Row type="flex" justify="space-around" class="code-row-bg">
                        <Col span="11" >
                          <FormItem label='初次通过时间：' prop='firstPassDate'>
                            <div v-if="mode === 'view'">{{dataToView.firstPassDate.substr(0,10)}}</div>
                            <DatePicker v-else  :editable='false'  placeholder="请选择初次通过时间"  class="w" v-model='dataToView.firstPassDate'
                            ></DatePicker>
                          </FormItem>
                        </Col>
                        <Col span="11" >
                          <FormItem label='标准化组织评审单位：' prop='assessOrganizer'>
                            <div v-if="mode === 'view'">{{dataToView.assessOrganizer}}</div>
                            <Input v-else  type='text' v-model='dataToView.assessOrganizer'></Input>
                          </FormItem>
                        </Col>
                      </Row>
                      <Row type="flex" justify="space-around" class="code-row-bg">
                        <Col span="11" >
                          <FormItem label='标准化评审单位：' prop='assessOrgName'>
                            <div v-if="mode === 'view'">{{dataToView.assessOrgName}}</div>
                            <Input v-else  type='text' v-model='dataToView.assessOrgName'></Input>
                          </FormItem>
                        </Col>
                        <Col span="11" >
                          <FormItem label='下次复评计划时间：' prop='reassessDate'>
                            <div v-if="mode === 'view'">{{dataToView.reassessDate.substr(0,10)}}</div>
                            <DatePicker v-else  :editable='false'  placeholder="请选择下次复评计划时间"  class="w" v-model='dataToView.reassessDate'
                            ></DatePicker>
                          </FormItem></Col>
                      </Row>
                      <Row type="flex" justify="space-around" class="code-row-bg">
                        <Col span="11" >
                          <FormItem label='通过OHSAS18000管理体系认证情况：' prop='passOhsas'>
                            <div v-if="mode === 'view'">{{dataToView.passOhsas}}</div>
                            <Input v-else  type='text' v-model='dataToView.passOhsas'></Input>
                          </FormItem>
                        </Col>
                        <Col span="11">
                          <FormItem label='填表人：' prop='registrant'>
                            <div v-if="mode === 'view'">{{dataToView.createUserName}}</div>
                            <Input v-else type='text' v-model='dataToView.registrant' readonly></Input>
                          </FormItem>
                        </Col>
                      </Row>
                      <Row type="flex" justify="space-around" class="code-row-bg">
                        <Col span="11">
                            <FormItem label='填表时间：' prop='registTime'>
                                <div v-if="mode === 'view'">{{renderViewDate(dataToView.createDate)}}</div>
                                <DatePicker v-else type="date" placeholder="请选择日期" class="w" v-model='dataToView.registTime' readonly></DatePicker>
                            </FormItem>
                        </Col>
                          <Col span="11">
                          </Col>
                      </Row>
                      <Row type="flex" justify="space-around" class="code-row-bg">
                        <Col span="23" >
                          <FormItem label='备注：' prop='notes'>
                            <div v-if="mode === 'view'">{{dataToView.notes}}</div>
                            <Input v-else  type='textarea' v-model='dataToView.notes'></Input>
                          </FormItem>
                        </Col>
                      </Row>
                    </Form>
                </Col>
            </Row>
            <div slot="footer">
                <Button v-if="mode != 'view'" type="primary"  class='ivu-btn-save' @click="submit('dialogForm')" >确定</Button>
                <Button v-if="mode != 'view'" type="primary"  class='ivu-btn-cancel' @click="handleCancel" >取消</Button>
                <Button v-if="mode === 'view'" class='ivu-btn-save' @click="dialogShow=false">关闭</Button>
            </div>
        </Modal>
    </Layout>
</template>

<script>
  import {Api} from '@/api/index.js'
  import {formatDate} from '@/tools/index'
  import _deepclone from 'lodash/cloneDeep'
  import { mapState, mapGetters, mapActions } from 'vuex'
  var cascaderDom = () => import('@/components/cascader-dom.vue');
  import pageCommonFunction from '@/components/pageCommonFunction.js'

  export default {
    components:{
      cascaderDom
    },
    computed:{
        ...mapState('trhg', {
            areaMap: state => state.area.areaMap?state.area.areaMap:[],
            areaTreeMap: state => state.area.areaTreeMap?state.area.areaTreeMap:{},
        }),
    },
    data() {
      return {
        ...this.createCommonDataField(),
        columns1: [
          { title: '序号', width: 80, fixed: 'left', align: 'center', render: this.indexColumnsRender},
          // { key:'perfAssessNum',title: '绩效考核责任书编号',  align: 'center', width: 100 },
          { key:'companyName',title: '企业名称',  align: 'center', minWidth:220 },
          { key:'companyAddress',title: '所在地',  align: 'center', width: 140, render: this.renderCellArea  },
          { key:'standardRand',title: '已通过安全标准化建设达标等级',  align: 'center', minWidth:150, },
          { key:'firstPassDate',title: '初次通过时间',  align: 'center', render: (h, params) => {
                  return h('span', formatDate(new Date(params.row.firstPassDate), "yyyy-MM-dd"))
          }, minWidth:120},
          { key:'assessOrganizer',title: '标准化组织评审单位',  align: 'center', minWidth:160 },
          { key:'assessOrgName',title: '标准化评审单位',  align: 'center', minWidth:160  },
          { key:'reassessDate',title: '下次复评时间计划',  align: 'center', render: (h, params) => {
              return h('span', formatDate(new Date(params.row.reassessDate), "yyyy-MM-dd"))
          }, minWidth:150},
          { key:'passOhsas',title: '通过OHSAS18000管理体系认证情况',  align: 'center', minWidth:150 },
            { key:'createDate',title: '填表时间',  align: 'left', width: 120,  render: this.renderCellDate },
            { key:'notes',title: '备注',  align: 'center', minWidth:200 , tooltip:true },
          this.getActionColumns()
        ],
        ruleCustom : {
          // perfAssessNum: [ { required: true, message: '请填写绩效考核责任书编号', trigger: 'change' } ],
            companyId: [ { required: true, message: '请填写企业名称', trigger: 'change' } ],
          companyAddress: [ { required: true, message: '请填写所在地', trigger: 'change' } ],
          standardRand: [ { required: true, message: '请填写已通过安全标准化建设达标等级', trigger: 'change' } ],
          firstPassDate: [ { required: true, message: '请填写初次通过时间', trigger: 'change', type:'date' } ],
          assessOrganizer: [ { required: true, message: '请填写标准化组织评审单位', trigger: 'change' } ],
          assessOrgName: [ { required: true, message: '请填写标准化评审单位', trigger: 'change' } ],
          reassessDate: [ { required: true, message: '请填写下次复评时间计划', trigger: 'change', type:'date' } ],
          passOhsas: [ { required: true, message: '请填写通过OHSAS18000管理体系认证情况', trigger: 'change' } ],
          // notes: [ { required: true, message: '请填写备注', trigger: 'change' } ],
          rptPeroid: [ { required: true, message: '请填写月份', trigger: 'change', type:'date' } ],
        },
        tableHeight:10,

        // api
        queryUrl:Api.querySafeRptStandard,
        exportUrl:Api.exportSafeRptStandard,
        deleteUrl:Api.deleteSafeRptStandard,
        bthDeleteUrl:Api.batchDeleteSafeRptStandard,
        getUrl:Api.getSafeRptStandard,
        updateUrl:Api.updateSafeRptStandard,
        saveUrl:Api.saveSafeRptStandard
      }
    },
    async mounted() {
      this.setTableHeight();
	  this.pageStatus.baseLoading = true
      await this.$store.dispatch('trhg/area/query', {scope:this}, { root: true })
	  this.pageStatus.baseLoading = false
      this.query();
        this.tempAreaTree = this.areaTreeMap['0']
    },
    methods:{
        ...pageCommonFunction,

        createEmptyDataToAdd() {
            return {
                companyAddressArr:[]
            }
        },
        beforeEditDialogOpen(data) {
            if(data.row.companyAddress){
                data.row.companyAddressArr = data.row.companyAddress.split(',')
                data.row.companyAddressName = this.areaCodeToAreaName(data.row.companyAddress)
            }
        },
        async afterDataToViewInit(data) {
            if (data && data.companyAddress) {
                let companyAddressArr = data.companyAddress.split(',')
                this.tempAreaTree = []
                setTimeout(() => {
                    this.tempAreaTree = this.addressEditArr(this.areaTreeMap,companyAddressArr)
                }, 100)
            }
        },
        deleteData(params) {
            this.deleteDateAjax(params);
        },
        query() {
            this.queryAjax();
        },
        submit(name) {
            this.$set(this.dataToView,'companyAddress',this.dataToView.companyAddressArr.join(','))
            this.submitAjax(name);
        },
    },
    watch:{
    },
  }
</script>

<style lang='css' scoped>
    .rpt-form{}
    /deep/ .rpt-form .query-form-el{width:100%;}
    /deep/ .rpt-form .ivu-form-item{margin-bottom: 24px;}
</style>
